package com.pet.item.controller;


import cn.hutool.core.bean.BeanUtil;
import com.pet.api.dto.ItemDTO;
import com.pet.api.dto.OrderDetailDTO;
import com.pet.common.domain.Result;
import com.pet.item.domain.po.Item;
import com.pet.item.domain.po.ItemInput;
import com.pet.item.domain.query.ItemQuery;
import com.pet.item.domain.vo.ItemDetailVO;
import com.pet.item.service.IItemService;
import com.pet.item.service.ISearchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.MutationMapping;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author Cookies
 * @since 2024-07-03
 */
@Api(tags = "商品信息")
@RestController
@RequestMapping("/items")
@RequiredArgsConstructor
public class ItemController {
    private final IItemService itemService;
    private final ISearchService searchService;

    @PostMapping("/list")
    @ApiOperation("分页根据店铺获取商品信息")
    public Result getItemsListByStoreId(@RequestBody ItemQuery itemQuery) {
        return Result.success(itemService.getItemsListByStoreId(itemQuery));
    }

    @GetMapping("/category/{category}")
    @ApiOperation("根据类别获取商品信息")
    public Result getItemsListByCategory(
            @PathVariable @ApiParam(value = "类别 (pet / supply / other)", required = true) String category) {
        return Result.success(itemService.getItemsListByCategory(category));
    }

    @GetMapping("/hot")
    @ApiOperation("获取前十热门商品信息")
    public Result getHotItems() {
        return Result.success(itemService.getHotItems());
    }

    @GetMapping("/recommend/{current}")
    @ApiOperation("分页获取推荐商品")
    public Result getRecommendItems(
            @PathVariable @ApiParam("当前页") Integer current) {
        return itemService.getRecommendItems(current);
    }

    @ApiOperation("根据id获取商品")
    @GetMapping("/{id}")
    public Result queryItemById(
            @PathVariable @ApiParam(value = "商品id", required = true) Integer id) {
        return Result.success(itemService.queryItemById(id));
    }

    @ApiOperation(value = "根据id查询商品", hidden = true)
    @GetMapping("/query/{id}")
    public ItemDTO getItemById(
            @PathVariable @ApiParam(value = "商品id", required = true) Integer id) {
        return itemService.queryItemById(id);
    }

    @ApiOperation(value = "根据id批量查询商品", hidden = true)
    @GetMapping("/queryBatch")
    public List<ItemDTO> getItemByIds(@RequestParam("ids") List<Integer> ids) {
        return itemService.getItemByIds(ids);
    }

    @PostMapping
    @ApiOperation("添加商品信息")
    public Result addItem(@RequestBody Item item) {
        boolean isSucceed = itemService.addItem(item);
        return isSucceed ? Result.success() : Result.error();
    }

    @PutMapping
    @ApiOperation("更新商品信息")
    public Result updateItem(@RequestBody Item item) {
        boolean isSucceed = itemService.updateById(item);
        return isSucceed ? Result.success() : Result.error();
    }

    @ApiOperation("批量扣减库存")
    @PutMapping("/stock/deduct")
    public void deductStock(@RequestBody List<OrderDetailDTO> items) {
        itemService.deductStock(items);
    }

    @ApiOperation("批量回退库存")
    @PutMapping("/stock/append")
    public void appendStock(@RequestBody List<OrderDetailDTO> items) {
        itemService.appendStock(items);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除商品信息")
    public Result deleteItem(@PathVariable @ApiParam(value = "商品id", required = true) Integer id) {
        boolean isSucceed = itemService.removeById(id);
        return isSucceed ? Result.success() : Result.error();
    }

    @PostMapping("/delete")
    @ApiOperation("批量删除商品信息")
    public Result batchDeleteItem(@RequestBody @ApiParam(value = "商品id集合", required = true) ArrayList<Integer> ids) {
        boolean isSucceed = itemService.removeByIds(ids);
        return isSucceed ? Result.success() : Result.error();
    }

    @PostMapping("/search")
    @ApiOperation(value = "es搜索商品信息", hidden = true)
    public Result searchItem(@RequestBody String keyword) {
        return Result.success(searchService.searchItem(keyword));
    }

    @QueryMapping
    public String hello() {
        return "hello graphql";
    }

    @QueryMapping
    public List<ItemDetailVO> getItemsByCategory(@Argument String category) {
        return itemService.getItemsListByCategory(category);
    }

    @MutationMapping
    public Item addItemByInput(@Argument ItemInput itemInput) {
        if (itemInput == null) {
            return null;
        }
        System.out.println(itemInput);
        Item item = BeanUtil.copyProperties(itemInput, Item.class);
        boolean isSuccess = itemService.addItem(item);
        return isSuccess ? item : null;
    }
}